package main

/*
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
*/
/*
rune
byte 等同于int8，常用来处理ascii字符
rune 等同于int32,常用来处理unicode或utf-8字符
*/
func isValid(s string) bool {
	if len(s) == 0 {
		return true
	}
	stack := make([]rune, 0)
	for _, v := range s {
		if (v == '[') || (v == '{') || (v == '(') {
			stack = append(stack, v)
		} else if (v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(' ||
			(v == ']') && len(stack) > 0 && stack[len(stack)-1] == '[' ||
			(v == '}') && len(stack) > 0 && stack[len(stack)-1] == '{' {
			//pop last one
			stack = stack[:len(stack)-1]
		} else {
			return false
		}
	}
	//是否完全匹配完
	return len(stack) == 0
}
